package cn.zftt.quartz.controller;

import cn.zftt.common.json.Json;
import cn.zftt.quartz.model.Job;
import cn.zftt.quartz.model.JobLog;
import cn.zftt.quartz.model.Page;
import cn.zftt.quartz.service.JobService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class JobController {

    private final JobService jobService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index() {
        return "index";
    }


    @ResponseBody
    @RequestMapping(value = "/jobs", method = RequestMethod.POST)
    public Json list(@RequestBody Job job) {
        Page<Job> jobs = null;
        try {
            jobs = jobService.find(job);
        } catch (SchedulerException e) {
            return Json.error(100, e.toString());
        }
        return Json.success().put("jobs", jobs).put("time", new Date());
    }

    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Json add(@RequestBody Job job) {
        try {
            jobService.add(job);
        } catch (ClassNotFoundException e) {
            return Json.error(111, e.getMessage());
        } catch (SchedulerException e) {
            return Json.error(123, e.getMessage());
        }
        return Json.success();
    }

    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Json remove(@RequestBody(required = true) Job job) {
        try {
            jobService.remove(job);
        } catch (SchedulerException e) {
            return Json.error(133, e.toString());
        }
        return Json.success();
    }

    @ResponseBody
    @RequestMapping(value = "/run", method = RequestMethod.POST)
    public Json runOnce(@RequestBody(required = true) Job job) {
        try {
            jobService.runOnce(job);
        } catch (SchedulerException e) {
            return Json.error(133, e.toString());
        }
        return Json.success();
    }

    @ResponseBody
    @RequestMapping(value = "/pause", method = RequestMethod.POST)
    public Json pause(@RequestBody(required = true) Job job) {
        try {
            jobService.pause(job);
        } catch (SchedulerException e) {
            return Json.error(133, e.toString());
        }
        return Json.success();
    }

    @ResponseBody
    @RequestMapping(value = "/resume", method = RequestMethod.POST)
    public Json resume(@RequestBody(required = true) Job job) {
        try {
            jobService.resume(job);
        } catch (SchedulerException e) {
            return Json.error(133, e.toString());
        }
        return Json.success();
    }

    @ResponseBody
    @RequestMapping(value = "/reschedule", method = RequestMethod.POST)
    public Json reschedule(@RequestBody(required = true) Job job) {
        try {
            jobService.reschedule(job);
        } catch (SchedulerException e) {
            return Json.error(133, e.toString());
        }
        return Json.success();
    }

    @ResponseBody
    @RequestMapping(value = "/logs", method = RequestMethod.POST)
    public Json logs(@RequestBody(required = true) JobLog jobLog) {
        Page page = jobService.logs(jobLog);
        return Json.success().put("rows", page.getRows()).put("total", page.getTotal());
    }

    @Autowired
    public JobController(JobService jobService) {
        this.jobService = jobService;
    }
}
